﻿using mars_server.csharp.DB;
using mars_server.csharp.model;
using mars_server.csharp.Util;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace mars_server.csharp.dao.impl
{
    public class UserGroupManagerImpl : UserGroupManager
    {
        private UserGroupManagerImpl() { }
        private static readonly UserGroupManagerImpl instance = new UserGroupManagerImpl();

        public static UserGroupManagerImpl Instance
        {
            get
            {
                return instance;
            }
        }

        string tableName = "mars_user_group";//这个manager对应的表
        ConnectionPool pool = ConnectionPool.getInstance();//mysql连接池，单例模式

        public List<UserGroupDO> selectUserGroupDOsByGroupId(long groupId)
        {
            MySqlConnection connection = pool.getConnection();
            List<UserGroupDO> userGroupDOs = new List<UserGroupDO>();
            string query = SqlUtil.selectByIntQuery(tableName, "group_id", groupId.ToString());
            try
            {
                MySqlCommand cmd = new MySqlCommand(query, connection);
                MySqlDataReader dataReader = cmd.ExecuteReader();
                while (dataReader.Read())
                {
                    UserGroupDO userGroupDO = new UserGroupDO();
                    this.dataReaderToUserGroupDO(userGroupDO, dataReader);
                    userGroupDOs.Add(userGroupDO);
                }
                dataReader.Close();
                pool.releaseConnection(connection);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());

            }
            finally
            {
                pool.releaseConnection(connection);
            }
            return userGroupDOs;
        }

        public bool insertUserGroupDO(UserGroupDO userGroupDO)
        {
            string query = "insert into mars_user_group values (" + "NULL" + ",'" + userGroupDO.userId + "','" + userGroupDO.GroupId + "'," + "NULL)";
            Console.WriteLine(query);
            MySqlConnection connection = pool.getConnection();
            try
            {
                MySqlCommand cmd = new MySqlCommand(query, connection);
                cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                pool.releaseConnection(connection);
                return false;
            }
            finally
            {
                pool.releaseConnection(connection);
            }
            return true;
        }

        public void dataReaderToUserGroupDO(UserGroupDO userGroupDO, MySqlDataReader dataReader)
        {
            userGroupDO.id = long.Parse(dataReader[0].ToString());
            userGroupDO.userId = long.Parse(dataReader[1].ToString());
            userGroupDO.GroupId = long.Parse(dataReader[2].ToString());
            userGroupDO.userRole = 0;
        }

        public List<UserGroupDO> selectGroupDOsByUserId(long userId)
        {
            MySqlConnection connection = pool.getConnection();
            List<UserGroupDO> userGroupDOs = new List<UserGroupDO>();
            string query = SqlUtil.selectByIntQuery(tableName, "user_id", userId.ToString());
            try
            {
                MySqlCommand cmd = new MySqlCommand(query, connection);
                MySqlDataReader dataReader = cmd.ExecuteReader();
                while (dataReader.Read())
                {
                    UserGroupDO userGroupDO = new UserGroupDO();
                    this.dataReaderToUserGroupDO(userGroupDO, dataReader);
                    userGroupDOs.Add(userGroupDO);
                }
                dataReader.Close();
                pool.releaseConnection(connection);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());

            }
            finally
            {
                pool.releaseConnection(connection);
            }
            return userGroupDOs;
            throw new NotImplementedException();
        }
    }
}
